package com.demo.web.controller;


import com.demo.service.Test.Testservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;


@RestController
@RequestMapping("/api/teacher")
public class teachercontroller {


  @Autowired
  private Testservice testservice;

//    @Autowired
//    private RedisTemplate redisTemplate;

//    @GetMapping("/getuser")
//    public List<User> getUserOne() {
//        List<User> list = new ArrayList<>();
//        User user = userservice.getUserOne(1L);
//        System.out.println(user);
//        list.add(user);
//        return list;
//    }


//    //map返回结果集
//    @PostMapping("/Login")
//    public HashMap<String,Object> Login(@RequestBody User user) {
//        HashMap<String, Object> map = new HashMap<>();
//        String token = null;
//        if (loginservice.verifyUserAndPassword(user.getUsername(),user.getPassword())>0) {
//            //登录成功生成token
////            Token tokenTool;
//            token = tokenTool.createToken();
//        }
//        map.put("message", "登录成功");
//        map.put("token", token);
//////        Serializable username=user.getUsername();
//////        //将token存入redis
//////        tokenTool.depositRedis(token, (String) username);
//////        ValueOperations valueOperations = redisTemplate.opsForValue();
//////        valueOperations.set(token,user.getUid(), 30, TimeUnit.MINUTES);
////        //将token存储到redis中
////        ValueOperations operations = stringRedisTemplate.opsForValue();
////        operations.set(token, user.getUsername());
////        //设置redis中该token的有效时间为20分钟
////        stringRedisTemplate.expire(token, 20, TimeUnit.MINUTES);
//        tokenTool.depositRedis(token,user.getUsername());
//
//        return map;
//    }




    @GetMapping("/info")
    public String test() {
        return "我是教师";
    }

    @GetMapping("/query")
    public String query() {
        return testservice.query();
    }

    @GetMapping("/add")
    public String add() {
        return testservice.add();
    }
    @GetMapping("/del")
    public String del() {
        return testservice.del();
    }
    @GetMapping("/update")
    public String update() {
        return testservice.update();
    }

//    @PostMapping("/register")
//    public ReturnResult register(@RequestBody Register register) throws UnsupportedEncodingException {
//        int result=registerService.Register(register);
//        Boolean flag=result>0?true:false;
//        return ReturnResult.isSuccessOrError(flag);
//    }


    //获取用户登录信息1
    @GetMapping("/user1")
    public Authentication test1(Authentication authentication) {
        return authentication;
    }

    //获取用户登录信息2
    @GetMapping("/user2")
    public Principal test2(Principal principal) {
        return principal;
    }

    //获取用户登录信息3 通过上下文方式获取
    @GetMapping("/user3")
    public Principal test3() {
        return SecurityContextHolder.getContext().getAuthentication();
    }


}
